package com.yuan.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.blog.entity.CommentEntity;
import com.yuan.blog.entity.UserEntity;
import com.yuan.blog.mapper.CommentMapper;
import com.yuan.blog.service.ICommentService;
import com.yuan.blog.service.LikeService;
import com.yuan.blog.utils.TimeAgoUtils;
import com.yuan.blog.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @program: com.yuan.blog
 * @description:
 * @author: yuan_boss
 * @create: 2022-12-05 21:38
 **/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper,CommentEntity> implements ICommentService {


    @Autowired
    private LikeService likeService;

    @Override
    public List<CommentVo> commentListByInvitataionId(UserEntity userEntity, Integer id) throws SQLException {
        // 1、根据帖子id查询，它下面的一级评论信息和作者信息
        List<CommentVo> commentVoList = baseMapper.commentListByInvitataionId(1, id);


        // 2.循环遍历，查询每一条评论下面的回复
        for (CommentVo commentVo : commentVoList) {

            // 3.查询当前评论的点赞数量
            Long invitataionLikeCount = likeService.getInvitataionLikeCount(2, commentVo.getComment2Vo().getId());

            commentVo.setLikeCount(invitataionLikeCount);
            commentVo.setLikeStatus(0);
            if (userEntity != null) {
                // 已经登录了，查询当前用户是否对评论点赞了
                Boolean invitataionUserLike = likeService.getInvitataionUserLike(2, userEntity.getId(), commentVo.getComment2Vo().getId());
                commentVo.setLikeStatus(invitataionUserLike ? 1 : 0);
            }
            Integer cid = commentVo.getComment2Vo().getId();

            commentVo.getComment2Vo().setCreateTimeText(TimeAgoUtils.format(commentVo.getComment2Vo().getCreateTime()));
            // 查询评论下面的回复
            List<CommentVo> replyList = baseMapper.commentListByInvitataionId(2, cid);

            // 遍历查询每个回复的点赞的数量
            for (CommentVo replyVo : replyList) {
                Long count = likeService.getInvitataionLikeCount(2, replyVo.getComment2Vo().getId());
                replyVo.setLikeCount(count);
                if (userEntity != null) {
                    // 已经登录了，查询当前用户是否对评论点赞了
                    Boolean invitataionUserLike = likeService.getInvitataionUserLike(2, userEntity.getId(), replyVo.getComment2Vo().getId());
                    replyVo.setLikeStatus(invitataionUserLike ? 1 : 0);
                }
                replyVo.getComment2Vo().setCreateTimeText(TimeAgoUtils.format(replyVo.getComment2Vo().getCreateTime()));
            }
            commentVo.setReplyList(replyList); // 评论下面的回复列表
        }

        return commentVoList;
    }

    @Override
    public Long getInvitationCommentSize(Integer id) {
        QueryWrapper<CommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("entity_type",1);
        wrapper.eq("entity_id",id);
        Long count = baseMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public boolean save(CommentEntity commentEntity){
        commentEntity.setCreateTime(new Date());
        commentEntity.setStatus(1);
        commentEntity.setId(null);
        return baseMapper.insert(commentEntity) > 0;
    }
}
